
// 鼠标长按指令

export default {
    inserted(el, binding) {
        var oDiv = el,
            value = binding.value,
            x = 0,
            y = 0,
            z = 0,
            timer = null;
        // 监听按下事件
        oDiv.addEventListener("touchstart", function(e) {
            if (e.touches.length > 1) {
                return false;
            }
            z = 0;
            timer = setTimeout(function() {
                z = 1;
                if(typeof value === 'function'){
                    value('start');
                }else{
                    console.log(value);
                }

            }, 2000);
            x = e.touches[0].clientX;
            y = e.touches[0].clientY;
            e.preventDefault();
        }, false);
        // 监听移动事件
        document.addEventListener("touchmove", function(e) {
            if (x != e.touches[0].clientX || y != e.touches[0].clientY) {
                clearTimeout(timer);
                return false;
            }
        }, false);
        // 监听松开实践
        document.addEventListener("touchend", function(ev) {
            if (z != 1) {
                clearTimeout(timer);
                x = 0;
                y = 0;
                return false;
            } else {
                x = 0;
                y = 0;
                z = 0;
                // alert("长按了啊")
                if(typeof value === 'function'){
                    value('end');
                }else{
                    console.log(value);
                }
            }
        }, false);
    }
}